package com.xm.settlement.controller;

import com.xm.settlement.commons.base.ApiSQLException;
import com.xm.settlement.commons.base.BaseController;
import com.xm.settlement.commons.utils.*;
import com.xm.settlement.model.User;
import com.xm.settlement.model.vo.AppUserVo;
import com.xm.settlement.service.AppUserService;
import com.xm.settlement.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * 查询VIp用户 on 2017/5/1.
 */

@Controller
@RequestMapping(value = "/appVip")
public class AppUserController extends BaseController {

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private IUserService userService;

    /**
     * @return
     */
    @GetMapping(value = "/manager")
    public String Manager() {
        return "settlement/appUser/appUser";
    }


    /**
     * list
     */
    @PostMapping("/dataGrid")
    @ResponseBody
    public Object dataGrid(Integer page, Integer rows, String sort, String order) {
        Long Lid = getUserId();
        PageInfo pageInfo = new PageInfo (page, rows, sort, order);
        Map<String, Object> condition = new HashMap<String, Object>();
        pageInfo.setCondition(condition);
        appUserService.selectDataGrid(pageInfo,Lid);
        return pageInfo;
    }


    /**
     * 添加权限页
     *
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "settlement/appUser/appUserAdd";
    }

    /**
     * 充值码
     *
     * @param
     * @param id
     * @return
     */
    @RequestMapping("/editPage")
    public String editPage(Model model, Long id) {
        User user = userService.selectById(id);
        model.addAttribute("user", user);
        return "settlement/appUser/editAppUserPage";
    }


    /**
     * 编辑部门
     *
     * @param user
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(User user) {
        user.setPassword(DigestUtils.md5Hex(user.getPassword()));
        userService.updateSelectiveById(user);
        return renderSuccess("编辑成功！");
    }


    /**
     * 删除部门
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        appUserService.deleteById(id);
        return renderSuccess("删除成功！");
    }


    /**
     * 上传
     *
     * @return
     */
    @GetMapping("/addUploadPage")
    public String addUploadPage() {
        return "settlement/appUser/appUserUploadAdd";
    }


    /**
     * 添加会员
     *
     * @param appUserVo
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Object add(AppUserVo appUserVo) {
        try {
            Long lid =  getUserId();
            appUserService.insertAppUser(appUserVo,lid);
        }catch ( InterruptedException i){
            i.printStackTrace();
        }catch (UnsupportedEncodingException u ){
            u.printStackTrace();
        }catch (URISyntaxException uri){
            uri.printStackTrace();
        }catch (ApiSQLException ap){
            ap.printStackTrace();
            return renderError(ap.getMessage());
        }
        return renderSuccess("添加成功！");
    }


    /**
     * 下载
     */
    @RequestMapping(value="/downloadExcel")
    public void downloadExcel(HttpServletResponse response)throws Exception{
        FileDownload.fileDownload(response, PathUtil.getClasspath() + Const.FILEPATHFILE + "appUser.xls", "appUser.xls");
    }


    /**
     * Excel导入
     *
     * */
    @RequestMapping(value="/uploadExcel",method= RequestMethod.POST)
    @ResponseBody
    public Object uploadExcel(@RequestParam("Filedata") MultipartFile multiFile, HttpServletRequest request, HttpServletResponse response){
        try {
            String filePath = PathUtil.getClasspath() + Const.FILEPATHFILE;								//文件上传路径
            String fileName =  FileUpload.fileUp(multiFile, filePath, System.currentTimeMillis()+"_appUser");
            appUserService.insertAppuserByExcel(filePath,fileName);
            return renderSuccess(  "上传成功" );
        }catch (ApiSQLException io){
            io.printStackTrace();
            logger.info(io);
            return renderError("上传失败" );
        }
    }

    /**
     * 会员管理
     * */
    /**
     * @return
     */
    @GetMapping(value = "/managerTree")
    public String managerTree() {
        return "settlement/appUser/appUserTree";
    }


    /**
     * list
     */
    @PostMapping("/dataGridTree")
    @ResponseBody
    public Object dataGridTree(String vipCode) {
        return  appUserService.selectDataGridTree(  vipCode,getUserId());
    }


    @GetMapping(value = "/appUserPage")
    public String appUserPage(Model model, String vipCode) {
        model.addAttribute("pushCode",vipCode);
        return "settlement/appUser/addAppUserPage";
    }


    @GetMapping(value = "/editUserPage")
    public String editUserPage(Model model, String login_name) {
        model.addAttribute("login_name",login_name);
        return "settlement/appUser/editAppUserPage";
    }



    @RequestMapping("/editOrigin")
    @ResponseBody
    public Object editOrigin(String vipCode,String organizationId ) {
        //修改当前组织下的全部结构平台 id
        appUserService.updateappEditOrigin(vipCode,organizationId) ;
        return renderSuccess("添加成功！");
    }

    @RequestMapping("/addAccount")
    @ResponseBody
    public Object addAccount(AppUserVo appUserVo) {
        try {
            appUserVo.setPhone(Long.valueOf(appUserVo.getVip_code()));
            appUserService.insertAccount(appUserVo,getShiroUser());
        }catch ( InterruptedException i){
            i.printStackTrace();
        }catch (UnsupportedEncodingException u ){
            u.printStackTrace();
        }catch (URISyntaxException uri){
            uri.printStackTrace();
        }catch (ApiSQLException ap){
            ap.printStackTrace();
            return renderError(ap.getMessage());
        }
        return renderSuccess("开户成功！");
    }
}
